﻿using Iworld.Backstage.Framework.AdministratorService;
using Iworld.ViewModel;
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Iworld.Backstage.Framework
{
    /// <summary>
    /// 管理员组列表视图模型
    /// </summary>
    public class AdministratorGroupListViewModel : ShowListViewModelBase<AdministratorGroupExport, AdministratorServiceClient>
    {
        string keyword;

        public AdministratorGroupListViewModel()
            : base(PageCategory.管理员管理.ToString(), Page.管理员组列表.ToString())
        {
            this.client.GetAdministratorGroupsCompleted += this.WrapEventHandler<GetAdministratorGroupsCompletedEventArgs>(GetAdministratorGroupsCompleted);
        }

        public string Keyword
        {
            get { return keyword; }
            set
            {
                if (keyword == value) { return; }
                keyword = value == "" ? null : value;
                OnPropertyChanged("Keyword");
                Refresh(1);
            }
        }

        void GetAdministratorGroupsCompleted(object sender, GetAdministratorGroupsCompletedEventArgs e)
        {
            if (e.Result.Success)
            {
                this.PageIndex = e.Result.PageIndex;
                this.TotalPage = e.Result.CountOfPage;
                UpdateLsit(e.Result.List);
            }
            else
            {
                ShowError(e.Result.Error);
            }
        }

        protected override void Refresh(object obj)
        {
            int pageIndex = obj == null ? this.PageIndex : Convert.ToInt32(obj);
            client.GetAdministratorGroupsAsync(new GetAdministratorGroupsImport()
            {
             Keyword=this.Keyword,
                PageIndex = pageIndex,
                Token = DataManager.GetValue<AdministratorExport>(DataKey.Administrator_Info).Token
            });
        }

        protected override void Reset(object obj)
        {
            Keyword = null;
            this.PageIndex = 1;
            Refresh(null);
        }
    }
}
